package com.lanxin.oa.approve.service;

import com.lanxin.bean.BxInformal;
import com.lanxin.bean.LcApproval;
import com.lanxin.bean.LcFlow;
import com.lanxin.bean.LcNode;
import com.lanxin.oa.approve.dao.FlowDao;
import com.lanxin.oa.approve.dao.NodeDao;
import com.lanxin.oa.fiancial.dao.ApplyDao;
import com.lanxin.util.PageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class FlowServiceImpl implements FlowService{
    @Autowired
    private FlowDao flowDao;

    @Autowired
    private NodeDao nodeDao;

    @Autowired
    private ApplyDao applyDao;

    @Override
    public PageBean listMyFlow(Integer page, Integer pageSize, HttpSession session, LcApproval approval) {
        return flowDao.selectMyFlow(approval, session, page, pageSize);
    }

    @Override
    public List<LcFlow> selectLcFlow() {
        return flowDao.selectLcFlow();
    }

    @Override
    public List<LcApproval> listApprovalJiLu(String docNo) {
        return flowDao.selectApproveJiLu(docNo);
    }

    @Override
    public PageBean listMyApproval(LcApproval approval, Integer empid, Integer page, Integer pageSize) {
        return flowDao.selectApprovalById(approval,empid,page,pageSize);
    }

    @Override
    public String getReadOnlyDocInfoURL(Integer docid) {
        return "apply/BxInfo?oid=" + docid;
    }

    @Override
    public void doAfterApproval(Integer docid) {
        BxInformal info = applyDao.selectByBxInFormalId(docid);
        info.setStatus("Y");
        applyDao.update(info);
    }

    @Override
    public void doAfterDisApproval(Integer docid) {
        BxInformal info = applyDao.selectByBxInFormalId(docid);
        info.setStatus("N");
        applyDao.update(info);
    }

    @Override
    public String docInfoUrl(Integer docid) {
        return getReadOnlyDocInfoURL(docid);
    }

    @Override
    public LcApproval approvalInfo(Integer oid) {
        return flowDao.approvalInfo(oid);

    }
    @Override
    public List<LcApproval> listApprovalByDocNo(String docNo) {
        return flowDao.listApprovalByDocNo(docNo);
    }


    @Override
    public void doApproval(LcApproval approval, Integer empid) {
        LcApproval oldApproval = flowDao.getApproval(approval.getOid());
        oldApproval.setStatus(approval.getStatus());
        oldApproval.setOpinion(approval.getOpinion());
        oldApproval.setApprovaluid(empid);
        oldApproval.setApprovalDate(new Date());
        flowDao.update(oldApproval);
        if ("Y".equals(approval.getStatus())) {
            LcNode nextNode = nodeDao.getNextNode(oldApproval.getNodeid());
            if (nextNode != null) {
                LcApproval newApproval = new LcApproval();
                BeanUtils.copyProperties(oldApproval, newApproval,new String[] { "oid", "nodeid", "approvaluid", "status", "opinion", "approvalDate" });
                newApproval.setNodeid(nextNode.getOid());
                newApproval.setStatus("S");
                flowDao.add(newApproval);
            } else {
                doAfterApproval(oldApproval.getDocid());
            }
        } else {
            doAfterDisApproval(oldApproval.getDocid());
        }
    }
}
